package com.ruoyi.quartz.task;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.personnelMatters.domain.*;
import com.ruoyi.personnelMatters.service.IPerScheduledTasksService;
import com.ruoyi.system.mapper.*;
import com.ruoyi.system.service.ISysUserPositionLevelHistoryService;
import com.ruoyi.system.service.ISysUserSalaryHistoryService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * 人员管理定时任务
 *
 * @author zy
 */
@Component("personScheduledTasks")
public class PersonScheduledTasks {

    @Autowired
    private IPerScheduledTasksService perScheduledTasksService;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private SysUserMapper userMapper;

    @Autowired
    private SysUserRoleMapper userRoleMapper;

    @Autowired
    private SysUserPostMapper userPostMapper;

    @Autowired
    private SysUserPositionMapper userPositionMapper;

    @Autowired
    private SysUserPositionLevelMapper userPositionLevelMapper;

    @Autowired
    private ISysUserPositionLevelHistoryService sysUserPositionLevelHistoryService;

    @Autowired
    ISysUserSalaryHistoryService sysUserSalaryHistoryService;

    /**
     * 扫岗位调整表
     */
    public void positionChangeScanInfo() {
        //扫岗位调整表到时间需要执行的信息
        List<PositionChangeScanInfo> toBeExecutInfoList = perScheduledTasksService.getToBeExecutPositionInfo();
        if (!toBeExecutInfoList.isEmpty()) {
            for (PositionChangeScanInfo toBeExecutInfo : toBeExecutInfoList) {
                Long newPositionId = toBeExecutInfo.getNewPositionId();
                Long newPositionLevelId = toBeExecutInfo.getNewPositionLevelId();
                Long userId = toBeExecutInfo.getUserId();
                //删除原来关联信息
                userPositionMapper.deleteUserPositionByUserId(userId);
                userPositionLevelMapper.deleteUserPositionLevelByUserId(userId);
                //新增关联信息
                userPositionMapper.insertUserPosition(newPositionId, userId);
                userPositionLevelMapper.insertUserPositionLevelByUserId(newPositionLevelId, userId);
                //新增或更新用户与岗位等级历史表
                if (StringUtils.isNotNull(newPositionLevelId)) {
                    sysUserPositionLevelHistoryService.insertOrUpdateUserPositionHistoryByUserId(newPositionLevelId, userId, new Date());
                }
                //标记该条记录已执行
                perScheduledTasksService.markPositionExecuted(toBeExecutInfo.getPersonPositionChangeId());
            }
        }
    }


    /**
     * 扫职务调整表
     */
    public void personPostChangeScanInfo() {
        //扫职务调整表到时间需要执行的信息
        List<PostChangeScanInfo> toBeExecutInfoList = perScheduledTasksService.getTobeExecutPostInfo();
        if (!toBeExecutInfoList.isEmpty()) {
            for (PostChangeScanInfo toBeExecutInfo : toBeExecutInfoList) {
                SysUser sysUser = new SysUser();
                sysUser.setUserId(toBeExecutInfo.getUserId());
                //转数组但只能塞一个
                Long[] newPostId = {toBeExecutInfo.getNewPostId()};
                sysUser.setPostIds(newPostId);
                sysUserService.updatePost(sysUser);
                //标记该条记录已执行
                perScheduledTasksService.markPostExecuted(toBeExecutInfo.getPersonPostChangeId());
            }
        }
    }


    /**
     * 扫科室调整表
     */
    public void personDeptChangeScanInfo() {
        List<DeptChangeScanInfo> toBeExecutInfoList = perScheduledTasksService.getToBeExecutDeptInfo();
        if (!toBeExecutInfoList.isEmpty()) {
            for (DeptChangeScanInfo toBeExecutInfo : toBeExecutInfoList) {
                SysUser sysUser = new SysUser();
                sysUser.setUserId(toBeExecutInfo.getUserId());
                sysUser.setDeptId(toBeExecutInfo.getNewDeptId());

                //执行user表部门调整
                userMapper.updateUser(sysUser);
                //标记该条记录已执行
                perScheduledTasksService.markExecuted(toBeExecutInfo.getPersonDeptChangeId());
            }
        } else {
            System.out.println("当前无调整记录");
        }
    }

    /**
     * 扫薪级调整表
     */
    public void personSalaryChangeScanInfo() {
        //扫薪资调整表到时间需要执行的信息
        List<SalaryChangeScanInfo> toBeExecutInfoList = perScheduledTasksService.getTobeExecuteSalaryChangeScanInfo();
        if (!toBeExecutInfoList.isEmpty()) {
            for (SalaryChangeScanInfo toBeExecutInfo : toBeExecutInfoList) {
                SysUser sysUser = new SysUser();
                sysUser.setUserId(toBeExecutInfo.getUserId());
                sysUser.setSalaryScale(toBeExecutInfo.getNewSalaryId());
                sysUserService.updateUserSalaryScale(sysUser);
                sysUserSalaryHistoryService.insertOrUpdateUserSalaryHistoryByUserId(toBeExecutInfo.getNewSalaryId(),toBeExecutInfo.getUserId(),new Date());
                //标记该条记录已执行
                perScheduledTasksService.markSalaryScaleExecuted(toBeExecutInfo.getPersonSalaryChangeId());
            }
        }
    }

    /**
     * 扫员工状态调整表
     */
    public void personEmployeeStatusChangeScanInfo() {
        //扫员工状态调整表到时间需要执行的信息
        List<EmployeeStatusScanInfo> toBeExecutInfoList = perScheduledTasksService.getTobeExecutEmployeeStatusInfo();
        if (!toBeExecutInfoList.isEmpty()) {
            for (EmployeeStatusScanInfo toBeExecutInfo : toBeExecutInfoList) {
                SysUser sysUser = new SysUser();
                sysUser.setUserId(toBeExecutInfo.getUserId());
                //设置要调整的员工状态
                sysUser.setEmployeeStatus(toBeExecutInfo.getNewEmpStatus());
                sysUserService.updateUserStatus(sysUser);
                //标记该条记录已执行
                perScheduledTasksService.markEmployeeStatusExecuted(toBeExecutInfo.getPersonEmpStatusChangeId());
            }
        }
    }

}
